from time import sleep
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey

desired_caps = {
  'platformName': 'Android', # 被测手机是安卓
  'platformVersion': '11', # 手机安卓版本  根据自己的版本来  不然的话启动不了的
  'deviceName': 'xxx', # 设备名，安卓手机可以随意填写
  'appPackage': 'com.taobao.taobao', # 启动APP Package名称
  'appActivity': 'com.taobao.tao.welcome.Welcome', # 启动Activity名称
  'noReset': True,       # 不要重置App
  'skipServerInstallation' : True,#第一次安装的时候注释掉 然后就打开  也可以True or False自己选择 ，就是要不要重新安装依赖文件
  'newCommandTimeout': 6000,
  'automationName' : 'UiAutomator2'
}
snumber = 0
s = 1
def func():
  global snumber
  driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
  try:
    driver.implicitly_wait(15)#就是找15秒  如果找不到就报错  在这之前会一直找 直到找到
    driver.find_element_by_accessibility_id("我的淘宝").click()
    driver.find_element_by_xpath('//android.widget.FrameLayout[@content-desc="我的淘宝"]/android.widget.ImageView').click()
    driver.find_element_by_xpath('//android.widget.FrameLayout[@content-desc="赢20亿红包"]').click()
    #driver.find_element_by_xpath('//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View[4]/android.view.View[2]/android.widget.Button').click()
    driver.find_element_by_android_uiautomator('new UiSelector().text("赚糖领红包")').click()
    #btn = driver.find_element_by_xpath('//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[4]/android.widget.ListView/android.view.View[2]/android.widget.Button')
    #btn = driver.find_element_by_xpath('//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.widget.ListView/android.view.View[2]/android.widget.Button')
    btn = driver.find_element_by_android_uiautomator('new UiSelector().text("去浏览")')
    try:
      while btn is not None:
        if(btn.text == "已完成"):
          break
        print("已获得"+ str(snumber) +"个毛毛骰子")
        btn.click()
        for j in range(460):
          sleep(0.05)
          print('\r' + '▇' * (j//17) + str(round(j/459*100, 2)) + '%', end='')
        print()
        driver.press_keycode(4)
        #btn = driver.find_element_by_xpath('//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[4]/android.widget.ListView/android.view.View[2]/android.widget.Button')
        #btn = driver.find_element_by_xpath('//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.bb/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.widget.ListView/android.view.View[2]/android.widget.Button')
        btn = driver.find_element_by_android_uiautomator('new UiSelector().text("去浏览")')
        snumber += 1
    except BaseException:
      return
    finally:
      driver.close()
  except BaseException:
    pass
  finally:
    driver.close()

while True:
  try:
    func()
    break
  except BaseException:
    pass
print("嘻嘻  小猫头子领完咯！！")
